package com.boot.controller;

import com.boot.entity.User;
import com.boot.service.MenuService;
import com.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    MenuService menuService;

    @GetMapping("/user/list")
    public String list(Model model){

        return "user/list";
    }

    @GetMapping("/user/delById")
    public String delById(Long id){
        userService.delById(id);
        return "redirect:/index";
    }


    @PostMapping("/user/deleteAll")
    @ResponseBody
    public String deleteAll(@RequestBody List<Long> ids){
        userService.deleteAll(ids);
        return "success";
    }

    @GetMapping("/user/add")
    public String add(Model model,Long id){
        model.addAttribute("menus",menuService.list());
        if(id!=null){
            model.addAttribute("user",userService.getById(id));
        }
        return "user/add";
    }

    @PostMapping("/user/doAdd")
    @ResponseBody
    public String doAdd(@RequestBody User user){
        userService.saveOrUpdate(user);
        return "success";
    }

}
